package com.xckj.cycling.view.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.cycling.R;
import com.xckj.cycling.adapter.SelectDateAdapter;
import com.xckj.cycling.framework.ResourcesAction;
import com.xckj.cycling.manager.PickerLayoutManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

/**
 * @Author: ChenYao
 * @Describe: 日期的Dialog
 * @Date: 2023/3/2 12:46
 */
public final class DateDialog {
    public static final class Builder extends AppCompatDialog implements Runnable, PickerLayoutManager.OnPickerListener, ResourcesAction, View.OnClickListener {
        private final int mStartYear;
        private final RecyclerView mYearView;
        private final RecyclerView mMonthView;
        private final RecyclerView mDayView;
        private final PickerLayoutManager mYearManager;
        private final PickerLayoutManager mMonthManager;
        private final PickerLayoutManager mDayManager;
        private final PickerAdapter mYearAdapter;
        private final PickerAdapter mMonthAdapter;
        private final PickerAdapter mDayAdapter;
        private MyOnClickListener mListener;

        public Builder(Context context) {
            this(context, Calendar.getInstance(Locale.CHINA).get(Calendar.YEAR) - 100);
        }

        public Builder(Context context, int startYear) {
            this(context, startYear, Calendar.getInstance(Locale.CHINA).get(Calendar.YEAR));
        }

        public Builder(Context context, int startYear, int endYear) {
            super(context);
            mStartYear = startYear;
            setContentView(R.layout.view_dialog_date);
            mYearView = findViewById(R.id.rv_date_year);
            mMonthView = findViewById(R.id.rv_date_month);
            mDayView = findViewById(R.id.rv_date_day);
            setCancelable(false);
            TextView btnSubmit = findViewById(R.id.btn_submit);
            ImageView ivClose = findViewById(R.id.iv_close);
            mYearAdapter = new PickerAdapter(context);
            mMonthAdapter = new PickerAdapter(context);
            mDayAdapter = new PickerAdapter(context);
            if (btnSubmit != null) {
                btnSubmit.setOnClickListener(this);
            }
            if (ivClose != null) {
                ivClose.setOnClickListener(this);
            }
            ArrayList<String> yearData = new ArrayList<>(10);
            for (int i = mStartYear; i <= endYear; i++) {
                yearData.add(i + " " + getString(R.string.year));
            }

            ArrayList<String> monthData = new ArrayList<>(12);
            for (int i = 1; i <= 12; i++) {
                monthData.add(i + " " + getString(R.string.month));
            }
            Calendar calendar = Calendar.getInstance(Locale.CHINA);
            int day = calendar.getActualMaximum(Calendar.DATE);
            // 生产天数
            ArrayList<String> dayData = new ArrayList<>(day);
            for (int i = 1; i <= day; i++) {
                dayData.add(i + " " + getString(R.string.day));
            }
            mYearAdapter.setData(yearData);
            mMonthAdapter.setData(monthData);
            mDayAdapter.setData(dayData);
            mYearManager = new PickerLayoutManager.Builder(context).build();
            mMonthManager = new PickerLayoutManager.Builder(context).build();
            mDayManager = new PickerLayoutManager.Builder(context).build();
            mYearView.setLayoutManager(mYearManager);
            mMonthView.setLayoutManager(mMonthManager);
            mDayView.setLayoutManager(mDayManager);
            mYearView.setAdapter(mYearAdapter);
            mMonthView.setAdapter(mMonthAdapter);
            mDayView.setAdapter(mDayAdapter);

            setYear(calendar.get(Calendar.YEAR));
            setMonth(calendar.get(Calendar.MONTH) + 1);
            setDay(calendar.get(Calendar.DAY_OF_MONTH));
            mYearManager.setOnPickerListener(this);
            mMonthManager.setOnPickerListener(this);
        }

        public Builder setYear(int year) {
            int index = year - mStartYear;
            if (index < 0) {
                index = 0;
            } else if (index > mYearAdapter.getCount() - 1) {
                index = mYearAdapter.getCount() - 1;
            }
            mYearView.scrollToPosition(index);
            refreshMonthMaximumDay();
            return this;
        }

        public Builder setMonth(int month) {
            int index = month - 1;
            if (index < 0) {
                index = 0;
            } else if (index > mMonthAdapter.getCount() - 1) {
                index = mMonthAdapter.getCount() - 1;
            }
            mMonthView.scrollToPosition(index);
            refreshMonthMaximumDay();
            return this;
        }

        public Builder setDay(int day) {
            int index = day - 1;
            if (index < 0) {
                index = 0;
            } else if (index > mDayAdapter.getCount() - 1) {
                index = mDayAdapter.getCount() - 1;
            }
            mDayView.scrollToPosition(index);
            refreshMonthMaximumDay();
            return this;
        }


        /**
         * 刷新每个月天最大天数
         */
        private void refreshMonthMaximumDay() {
            mYearView.removeCallbacks(this);
            mYearView.post(this);
        }

        public Builder setListener(MyOnClickListener onClickListener) {
            mListener = onClickListener;
            return this;
        }

        @Override
        public void run() {
            Calendar calendar = Calendar.getInstance(Locale.CHINA);
            calendar.set(mStartYear + mYearManager.getPickedPosition(), mMonthManager.getPickedPosition(), 1);

            int day = calendar.getActualMaximum(Calendar.DATE);
            if (mDayAdapter.getCount() != day) {
                ArrayList<String> dayData = new ArrayList<>(day);
                for (int i = 1; i <= day; i++) {
                    dayData.add(i + " " + getString(R.string.day));
                }
                mDayAdapter.setData(dayData);
            }
        }

        @Override
        public void onPicked(RecyclerView recyclerView, int position) {
            refreshMonthMaximumDay();
        }

        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_submit:
                    dismiss();
                    if (null == mListener) {
                        return;
                    }
                    mListener.onSelected(mStartYear + mYearManager.getPickedPosition(), mMonthManager.getPickedPosition() + 1, mDayManager.getPickedPosition() + 1);
                    break;
                case R.id.iv_close:
                    dismiss();
                    break;
                default:
                    break;
            }
        }

        private static final class PickerAdapter extends SelectDateAdapter<String> {

            private PickerAdapter(Context context) {
                super(context);
            }

            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new ViewHolder();
            }

            private final class ViewHolder extends SelectDateAdapter<?>.ViewHolder {

                private final TextView mPickerView;

                ViewHolder() {
                    super(R.layout.picker_item);
                    mPickerView = findViewById(R.id.tv_picker_name);
                }

                @Override
                public void onBindView(int position) {
                    mPickerView.setText(getItem(position));
                }
            }
        }
    }

    public interface MyOnClickListener {
        /**
         * 选择完日期后回调
         *
         * @param year  年
         * @param month 月
         * @param day   日
         */
        void onSelected(int year, int month, int day);
    }
}